﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Stashy.Models;
using StashyLib;

namespace Stashy.Controllers
{ 
    public class BugController : Controller
    {
        private IStashy<int> db;

        public BugController(IStashy<int> stashy)
        {
            this.db = stashy;
        }

        //
        // GET: /Bug/

        public ViewResult Index()
        {
            return View(db.LoadAll<Bug>());
        }

        //
        // GET: /Bug/Details/5

        public ViewResult Details(int id)
        {
            return View(db.Load<Bug>(id));
        }

        
        //
        // GET: /Bug/Edit/5
 
        public ActionResult Edit(int? id)
        {
            return View(id.HasValue? db.Load<Bug>(id.Value) : new Bug() { Id = db.GetNewId<Bug>()} );
        }

        //
        // POST: /Bug/Edit/5

        [HttpPost]
        public ActionResult Edit(Bug bug)
        {
            if (ModelState.IsValid)
            {
                db.Save<Bug>(bug, bug.Id);
                return RedirectToAction("Index");
            }
            return View(bug);
        }

        //
        // GET: /Bug/Delete/5
 
        public ActionResult Delete(int id)
        {
            return View(db.Load<Bug>(id));
        }

        //
        // POST: /Bug/Delete/5

        [HttpPost, ActionName("Delete")]
        public ActionResult DeleteConfirmed(int id)
        {
            db.Delete<Bug>(id);
            return RedirectToAction("Index");
        }
    }
}